/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import java.sql.*;
import java.util.ArrayList;

/**
 *
 * @author thien_000
 */
public class bean_ve {

    private String MaVe;
    private String MaCB;
    private String LoaiVe;
    private boolean TrangThai;
    private String GiaVe;

    public bean_ve() {

    }

    public bean_ve(String mave, String macb, String loaive, boolean trangthai, String giave) {
        this.GiaVe = giave;
        this.LoaiVe = loaive;
        this.MaCB = macb;
        this.MaVe = mave;
        this.TrangThai = trangthai;
    }

    public bean_ve(String loaive, boolean trangthai, String giave) {
        this.GiaVe = giave;
        this.LoaiVe = loaive;
        this.TrangThai = trangthai;
    }

    /**
     * @return the MaVe
     */
    public String getMaVe() {
        return MaVe;
    }

    /**
     * @return the MaCB
     */
    public String getMaCB() {
        return MaCB;
    }

    /**
     * @return the LoaiVe
     */
    public String getLoaiVe() {
        return LoaiVe;
    }

    /**
     * @return the TrangThai
     */
    public boolean isTrangThai() {
        return TrangThai;
    }

    /**
     * @return the GiaVe
     */
    public String getGiaVe() {
        return GiaVe;
    }

    /**
     * @param MaVe the MaVe to set
     */
    public void setMaVe(String MaVe) {
        this.MaVe = MaVe;
    }

    /**
     * @param MaCB the MaCB to set
     */
    public void setMaCB(String MaCB) {
        this.MaCB = MaCB;
    }

    /**
     * @param LoaiVe the LoaiVe to set
     */
    public void setLoaiVe(String LoaiVe) {
        this.LoaiVe = LoaiVe;
    }

    /**
     * @param TrangThai the TrangThai to set
     */
    public void setTrangThai(boolean TrangThai) {
        this.TrangThai = TrangThai;
    }

    /**
     * @param GiaVe the GiaVe to set
     */
    public void setGiaVe(String GiaVe) {
        this.GiaVe = GiaVe;
    }

    public bean_ve[] hienthive() {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        try {
            con = Connect.GetConnect();
            String sql = "SELECT * FROM ve";
            stm = con.prepareStatement(sql);
            rs = stm.executeQuery();
            ArrayList list = new ArrayList();

            while (rs.next()) {
                String mave = rs.getString("MaVe");
                String macb = rs.getString("MaCB");
                String loaive = rs.getString("LoaiVe");
                boolean trangthai = rs.getBoolean("TrangThai");
                String giave = rs.getString("GiaVe");
                bean_ve ve = new bean_ve(mave, macb, loaive, trangthai, giave);
                list.add(ve);
            }
            bean_ve[] result = new bean_ve[list.size()];
            list.toArray(result);
            rs.close();
            stm.close();
            con.close();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public bean_ve[] searchLikeMaVe(String MaVe) {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        try {
            con = Connect.GetConnect();
            String sql = "SELECT * FROM ve WHERE MaVe LIKE ?";
            stm = con.prepareStatement(sql);
            stm.setString(1, "%" + MaVe + "%");
            rs = stm.executeQuery();
            ArrayList list = new ArrayList();

            while (rs.next()) {
                String mave = rs.getString("MaVe");
                String macb = rs.getString("MaCB");
                String loaive = rs.getString("LoaiVe");
                boolean trangthai = rs.getBoolean("TrangThai");
                String giave = rs.getString("GiaVe");
                bean_ve ve = new bean_ve(mave, macb, loaive, trangthai, giave);
                list.add(ve);
            }
            bean_ve[] result = new bean_ve[list.size()];
            list.toArray(result);
            rs.close();
            stm.close();
            con.close();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean insert(String mave, String macb, String loaive, boolean trangthai, String giave) {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = Connect.GetConnect();
            String sql = "INSERT INTO ve(MaVe,MaCB,LoaiVe,TrangThai,GiaVe)" + "VALUES(?,?,?,?,?)";
            stm = con.prepareStatement(sql);
            stm.setString(1, mave);
            stm.setString(2, macb);
            stm.setString(3, loaive);
            stm.setBoolean(4, trangthai);
            stm.setString(5, giave);
            int result = stm.executeUpdate();
            stm.close();
            con.close();
            if (result > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean deleteVe() {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = Connect.GetConnect();
            String sql = "DELETE FROM ve WHERE MaVe=?";
            stm = con.prepareStatement(sql);
            stm.setString(1, MaVe);
            int result = stm.executeUpdate();
            stm.close();
            con.close();
            if (result > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean updateVe() {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = Connect.GetConnect();
            String sql = "UPDATE ve SET MaCB=?,LoaiVe=?,TrangThai=?,GiaVe=? WHERE MaVe=?";
            stm = con.prepareStatement(sql);
            stm.setString(1, MaCB);
            stm.setString(2, LoaiVe);
            stm.setBoolean(3, TrangThai);
            stm.setString(4, GiaVe);
            stm.setString(5, MaVe);
            int result = stm.executeUpdate();
            stm.close();
            con.close();
            if (result > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean deleteAll() {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = Connect.GetConnect();
            String sql = "DELETE FROM ve";
            stm = con.prepareStatement(sql);
            int result = stm.executeUpdate();
            stm.close();
            con.close();
            if (result > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
